<?php
require_once ('..'.DIRECTORY_SEPARATOR.'initialize.php');

include_once(Utils::getPath(array('repositories', 'userRepository.php')));
include_once(Utils::getPath(array('repositories', 'messageRepository.php')));

$idmsg = $_GET['id'];

$userRep = new UserRepository();
$messageRep = new MessageRepository();
$msg = $messageRep->getById($idmsg);
$referer = getenv("HTTP_REFERER");

if (isset($idmsg) && isset($msg) && $msg->getIdSender() == $_SESSION['authenticatedId']) {
    $messageRep->remove($msg);
    Utils::showInfo('Message deleted!');
    header("Location: ".Utils::getUrl('message/list.php'));
    exit();
} else if(!$msg->getIdSender() != $_SESSION['authenticatedId']){
    Utils::goToErrorPage(StatusCodes::HTTP_FORBIDDEN);
}else{
    Utils::goToErrorPage();
}
?>